package GUI;


import java.util.ArrayList;

import Control.Shape;
import Control.Shape.Category;

public class ShapesPanel {
	public int x;
	public int y;
	public int width;
	public int height;
	public ArrayList<Button> shapesButtons;
	public float buttonTimer;
	public int heightVal;
	public boolean shapesDrawn;
	public Button circularButton;
	public Button polygonalButton;
	public Button lightFireButton;
	public Button otherButton;

	public int activeCategory = 4;

	public final int SHAPE_HEIGHT = 25;

	public int dropdownHeight=64;
	public float delay =0;
	int start=0;
	int end=0;

	public ShapesPanel() {
		super();
		this.x = 131;
		this.y = 0;
		this.width = 650-this.x-1;
		this.height = 30;
		this.shapesButtons = new ArrayList<Button>();



		for(int i=0;i<GV.shapes.size();i++){
			Button b = new Button();
			b.label = GV.shapes.get(i).getName();
			b.shapeButton=true;
			shapesButtons.add(b);
		}

		circularButton = new Button("Circular");
		polygonalButton = new Button("Polygonal");
		lightFireButton = new Button("Light Fire");
		otherButton = new Button("Other");
		circularButton.x = x+5;
		circularButton.y=1;
		circularButton.width = 100;
		polygonalButton.x = circularButton.x +105;
		polygonalButton.y=1;
		polygonalButton.width =100;
		lightFireButton.x = polygonalButton.x+105;
		lightFireButton.y=1;
		lightFireButton.width=100;
		otherButton.x = lightFireButton.x +105;
		otherButton.y = 1;
		otherButton.width =100;

	}


	public void updateActiveCategory(){

		if(circularButton.state && activeCategory!=Category.CIRCULAR.ordinal()){
			activeCategory=Category.CIRCULAR.ordinal();
			polygonalButton.state=false;
			lightFireButton.state=false;
			otherButton.state=false;
		}
		else if(polygonalButton.state && activeCategory!=Category.POLYGONAL.ordinal()){
			activeCategory=Category.POLYGONAL.ordinal();
			circularButton.state=false;
			lightFireButton.state=false;
			otherButton.state=false;
		}
		else if(lightFireButton.state && activeCategory!=Category.LIGHT.ordinal()){
			activeCategory=Category.LIGHT.ordinal();
			circularButton.state=false;
			polygonalButton.state=false;
			otherButton.state=false;
		}
		else if(otherButton.state && activeCategory!=Category.OTHER.ordinal()){
			activeCategory=Category.OTHER.ordinal();
			circularButton.state=false;
			polygonalButton.state=false;
			lightFireButton.state=false;
		}

		if(!circularButton.state && !polygonalButton.state && !lightFireButton.state && !otherButton.state){
			activeCategory=4;
		}
	}

	public void draw(float dt)
	{

		drawBackGroud();
		circularButton.draw();
		polygonalButton.draw();
		lightFireButton.draw();
		otherButton.draw();

		colorCategoryButtons();

		updateActiveCategory();


		if(activeCategory!=4){
			if(buttonTimer==(float)0.0){
				buttonTimer = GV.timer;
				heightVal=0;
			}
			heightVal = (int)GV.processing.lerp(heightVal,dropdownHeight,(float) 0.2);
			drawShapesCategoryBar();
		}
		else{
			if(buttonTimer!=(float)0.0){
				heightVal=dropdownHeight;
			}
			heightVal = (int)GV.processing.lerp(heightVal,0,(float) 0.2);
			drawShapesCategoryBar();
			buttonTimer=(float)0.0;

		}

	}

	private void colorCategoryButtons() {
		if(!GV.shapeCategoryColor){

			GV.processing.stroke(0);
			
			GV.processing.fill(GV.categoryCol[0]);
			GV.processing.ellipse(circularButton.x+circularButton.width +8, circularButton.y+17,12,12);

			GV.processing.fill(GV.categoryCol[1]);
			GV.processing.ellipse(polygonalButton.x+polygonalButton.width +8, polygonalButton.y+17,12,12);

			GV.processing.fill(GV.categoryCol[2]);
			GV.processing.ellipse(lightFireButton.x+lightFireButton.width +8, lightFireButton.y+17,12,12);

			GV.processing.fill(GV.categoryCol[3]);
			GV.processing.ellipse(otherButton.x+otherButton.width +8, otherButton.y+17,12,12);



		}		
	}


	private void drawShapesCategoryBar(){
		if(heightVal==0)return;

		GV.processing.rectMode(GV.processing.CORNER);
		GV.processing.fill(GV.backGroundColor,160);
		GV.processing.stroke(80);
		GV.processing.strokeWeight(2);
		GV.processing.rect(x, y+height, width, heightVal);
		GV.processing.textFont(GV.plotFont, 14);
		GV.processing.fill(GV.textColor);	
		GV.processing.textAlign(GV.processing.LEFT);

		switch (activeCategory){
		case 0: start = Shape.firstCircular; end = Shape.lastCircular;   break;
		case 1: start = Shape.firstPolygonal; end = Shape.lastPolygonal; break;
		case 2: start = Shape.firstLight; end = Shape.lastLight;   break;
		case 3: start = Shape.firstOther; end = Shape.lastOther;   break;
		}
		int k;
		for(int i=start; i<=end && ((i-start)/5)<heightVal/25;i++){
			k = (i-start)/5;
			Button b = shapesButtons.get(i);
			b.x =x +((i-start)%5)*100+5;
			b.y =y + (k+1)*30;
			b.draw();
			if(b.state){
				if(GV.shapeCategoryColor){
					GV.processing.fill(GV.shapeCol[GV.shapes.get(i).getShapeIndex().ordinal()]);}
				else{GV.processing.fill(GV.categoryCol[GV.shapes.get(i).getCategory().ordinal()]);}
				GV.processing.ellipse(b.x+b.width +8, b.y+17,12,12);
			}
		}
	}

	private void drawBackGroud() {
		GV.processing.strokeWeight(2);
		GV.processing.rectMode(GV.processing.CORNER);
		GV.processing.fill(GV.backGroundColor);
		GV.processing.stroke(80);
		GV.processing.rect(x,y,width,height);
	}

	public boolean mouseOver()
	{
		if(GV.processing.mouseX > x && GV.processing.mouseX < x + width && 
				GV.processing.mouseY > y && GV.processing.mouseY < y + height) 	return true;
		return false;
	}

	public boolean mouseOverArea()
	{
		if(GV.processing.mouseX > x && GV.processing.mouseX <  x+width && 
				GV.processing.mouseY > 0 && GV.processing.mouseY < height+dropdownHeight ) 	return true;
		return false;
	}

	public void updateShapesPanelButtonsState() {
		for(int i=0;i<GV.shapes.size();i++){
			Shape s = GV.shapes.get(i);
			Button b = 	shapesButtons.get(i);
			if(GV.shapesSelected.contains(s))
				b.state=true;
			else b.state =false;
		}
	}


	public void removeShape(Shape s){
		GV.shapesSelected.remove(s);
		GV.shapesPanel.updateShapesPanelButtonsState();
	}
	public void addShape(Shape s){
		GV.shapesSelected.add(s);
		GV.shapesPanel.updateShapesPanelButtonsState();

	}
	public void addAllShapes(){
		for(int i=0;i<GV.shapes.size();i++){
			Shape s = GV.shapes.get(i);
			if(!GV.shapesSelected.contains(s)){GV.shapesSelected.add(s);}
		}
		GV.shapesPanel.updateShapesPanelButtonsState();
	}
	public void removeAllShapes(){
		GV.shapesSelected = new ArrayList<Shape>();
		GV.shapesPanel.updateShapesPanelButtonsState();

	}

}
